# Virtual Router Simulator 
#
# Copyright (C) 2009-2010 VRSim Project
# Author: Choonho Son <choonho@kt.com>
# URL: <http://vrsim.googlecode.com>
# For license information, see LICENSE.TXT
#

from datetime import datetime
import VRSim.Globals as globals
from PyQt4 import QtGui

class NodesLog(QtGui.QTableWidget):
    def __init__(self,  parent):
        QtGui.QTableWidget.__init__(self,  parent)
        
        self.setColumnWidth(0,  150)
        self.setColumnWidth(1,  50)
        self.setColumnWidth(2,  320)
        
        self.log = []
        self.refresh()
        
    def refresh(self):
        self.setColumnWidth(0,  200)
        self.setColumnWidth(1,  50)
        self.setColumnWidth(2,  470)
        
        
        # header label
        self.setHorizontalHeaderLabels(["Time", "Type",  "Event"])
        self.setRowCount(len(self.log))
        
        # display reserver order
        count = len(self.log)
        for index in range(count):
            item = self.log[count - index -1]
            # item = ['time', type, 'event']
        
            item1 = QtGui.QTableWidgetItem(str(item[0]) )
            item2 = QtGui.QTableWidgetItem(str(item[1]) )
            if type(item[2]) != unicode:
                item3 = QtGui.QTableWidgetItem(unicode(item[2]))
            else:
                item3 = QtGui.QTableWidgetItem(unicode(item[2]))
            self.setItem(index, 0,  item1)
            self.setItem(index, 1,  item2)
            self.setItem(index, 2,  item3)
            self.setRowHeight(index,  20)
            
        
        
    def addEvent(self,  msg):
        dt = datetime.now()
        time = str(dt.strftime("%c"))
        if type(msg) != unicode:
            msg = unicode(msg, errors='replace')
        self.log.append([time, "log",  msg])
        
        # update windows
        self.refresh()
        
        
    def addError(self,  err):
        dt = datetime.now()
        time = str(dt.strftime("%c"))
        if type(err) != unicode:
            err = unicode(err, errors='replace')
        self.log.append([time,  "error",  err])
        
        # update windows
        self.refresh()
        
        # focus to Error Tab
        globals.GApp.mainWindow.tabWidget_Summary.setCurrentIndex(1)
